* Written by Jiro Yoshida
*2013/4/14

log using varcov, replace smcl

* This do file re-estimates RRI for a common time period
* for major MSAs, and retrieve the variance-covariance matrix for each MSA.
* The time period is adjusted for the shortest among major MSAs, which is San Francisco (group 163).
*

*major MSAs
*group	msa_name4	# of Obs
*9	Atlanta 	170046
*24	Boston  	6624
*48	Dallas  	94400
*55	Detroit  	4967
*81	Houston  	91928
*101	Los_Angel	33296
*108	Miami   	20361
*122	New_York	6330
*163	San_Franc	18225
*169	Seattle  	43488
*193	Washingto	28716


clear
cd /gpfs/home/juy18/work/rent_index
set matsize 800
set more off

use full.dta 


* keeping only major MSAs
keep if group==9| group==24| group== 48| group== 55| group== 81| group== 101| group== 108| group== 122| group== 163| group== 169| group== 193


tostring last, generate(leasestring)
gen lnrent=ln(rent)
gen leasedate=date(leasestring, "YMD")
gen leasemonth=mofd(leasedate)
gen leasequarter=qofd(leasedate)
gen leaseyear=yofd(leasedate)

/*establishing variables needed for repeat sales regression*/
sort unit_id leasedate 
gen apprate=lnrent-lnrent[_n-1] if unit_id[_n-1]==unit_id
gen firstquarter=leasequarter[_n-1] if unit_id==unit_id[_n-1]
gen secondquarter=leasequarter if unit_id==unit_id[_n-1]
gen time=1960+(leasequarter-1)/4

* using the common period as for San Francisco
keep if firstquarter >= 174

save majormsa_short, replace


foreach i in  9 24 48 55 81 101 108 122 163 169 193{   /// 

use majormsa_short.dta

keep if group==`i'
local msaorig= msa
*keep if msa==12060


*correction from here april 24 2012
quietly summarize firstquarter
local min=r(min)
quietly summarize secondquarter
local max=r(max)
*correction end here 
/* a clumsy trick to get the graphs right*/
gen minplusone=`min'+1
quietly summarize minplusone
local minplusone=r(mean)
/*this creates the repeat sales variables-- -1 for first sales, +1 for second*/
forvalues j=`min'/`max'  {
	gen rsquarter`j'=(secondquarter==`j')-(firstquarter==`j') 
	}
/*the coefficients in the following regression are the mean log rents by quarter (relative to the first sample in the quarter) for the whole sameple--the variable meanrent is the resulting series, renormalized to quarter 185*/
gen meanrent=0
xi:regress lnrent i.leasequarter

	forvalues j=`minplusone'/`max' {
	gen gamma`j'=_b[_Ileasequar_`j']  
	replace meanrent=gamma`j' if leasequarter==`j'
}
gen normfactormn=_b[_Ileasequar_185]
gen normmeanrent=(meanrent-normfactormn)
/*this command drops observations that are not well-defined, or are not repeat sales (in which case both first and second quarter==.*/
drop if firstquarter==secondquarter
gen rsindex=0
/*the repeat sales regression, followed by the lining up of the coefficient into a series (rsindex) to form the repeat sales index, also normalized to quarter 185*/
regress apprate rsquarter*,nocon 
predict rshat
forvalues j=`min'/`max'  {
	gen beta`j'= _b[rsquarter`j'] 
	replace rsindex=beta`j' if leasequarter==`j'	
	}
	

gen normfactorrs=_b[rsquarter185]
gen normrsindex=(rsindex-normfactorrs)
sort leasequarter

* showing the variance-covariance matrix
tab msa_name4
estat vce

/*WHAT CITY IS THIS?*/

local msa=msa_name4
*local msa "Atlanta"

*scatter normmeanrent normrsindex leasequarter, connect(l l) *title(`msa')

/*these commands to follow implement the Case-Shiller (really, FHFA) correction for heteroskedasticity for the repeat sales regression; these are collected in the series normgrsindex*/ 

gen esq=(apprate-rshat)^2
gen gap=secondquarter-firstquarter
gen gapsq=gap*gap
quietly reg esq gap gapsq
predict esqhat
regress apprate rsquarter* [aweight=1/esqhat], nocon
gen grsindex=0
gen grsindex_l=0
gen grsindex_u=0

forvalues j=`min'/`max'  {
	gen delta`j'= _b[rsquarter`j'] 
	
	gen se_l`j'= _se[rsquarter`j'] 
	gen delta_l`j'= _b[rsquarter`j']-1*_se[rsquarter`j'] 
	
	gen delta_u`j'= _b[rsquarter`j']+1*_se[rsquarter`j'] 
	
	replace grsindex=delta`j' if leasequarter==`j'	
	replace grsindex_l=delta_l`j' if leasequarter==`j'	
	replace grsindex_u=delta_u`j' if leasequarter==`j'	
	
	}
gen normfactorgrs=_b[rsquarter185]
gen normfactorgrs_l= _b[rsquarter185]-2*_se[rsquarter185]
gen normfactorgrs_u= _b[rsquarter185]+2*_se[rsquarter185]

gen normgrsindex=(grsindex-normfactorgrs)
gen normgrsindex_l=(grsindex_l-normfactorgrs)
gen normgrsindex_u=(grsindex_u-normfactorgrs)



*****
cd /gpfs/home/juy18/work/rent_index/varcov
save `msa', replace

cd /gpfs/home/juy18/work/rent_index

}

log close
